home *** CD-ROM | disk | FTP | other *** search
/ PD ROM 1 / PD ROM Volume I - Macintosh Software from BMUG (1988).iso / Stacks / Updates⁄New / TEXAS for BMUG / C progs / TEXAS XFCNs ƒ / mergeIndicesXFCN ƒ / *mergeIndicesXFCN.z1.c next >
Encoding:
Text File  |  1987-12-31  |  6.2 KB  |  218 lines  |  [TEXT/KAHL]

  1. /* mergeIndices XFCN -- 871230-... by ^z ... adapted from earlier
  2.  * non-XFCN mergeIndices work.... call it resource XFCN number 2806 or so.
  3.  *
  4.  * called from HyperCard as:     mergeIndices (x0, y0, buffersize)
  5.  *
  6.  * where x0 and y0 are the horizontal and vertical offsets (from the top
  7.  * left screen corner) for the files dialogs, and where the total buffer
  8.  * size is 200kB or so (whatever you have comfortably free when running
  9.  * HyperCard).
  10.  *
  11.  * The function returns nothing if there is an error, but returns "1"
  12.  * on success....
  13.  */
  14.  
  15.  
  16. /* mergeIndices.1.c -- by Mark^Zimmermann, 19871203-....
  17.  *
  18.  *     This little effort lets you merge together two indexed 'brwsr'
  19.  * format databases ... it asks you for two text file names, and
  20.  * then looks for those names with '.k' and '.p' endings, the related
  21.  * 'key' and 'ptr' files containing key words alphabetized and pointers
  22.  * to every occurrence of every word ... if it finds all of them (or
  23.  * if the user gives alternative names for the moved *.k and/or *.p files),
  24.  * it asks for a new name for the merged file, and then proceeds to
  25.  * join the original files together, thus:
  26.  *  - the two pairs of index files are merged, with the pointers from
  27.  * the second '.p' file adjusted to allow for the length of the first
  28.  * text file which will be preceding their target words, and with the
  29.  * cumulative count values in the merged '.k' file adjusted to allow
  30.  * for the new words that are in the second text file;
  31.  *  - if the merge of key and ptr files is successful, the original
  32.  * '.k' and '.p' files are then deleted;
  33.  *  - the second text file is appended to the end of the first (see
  34.  * my old 'append.c' program);
  35.  *  - if that merge is successful, the second text file is deleted and
  36.  * the first is renamed to the user's request.
  37.  *
  38.  * This approach still requires a considerable amount of free space
  39.  * on the disk, probably at least equal to the sum of the sizes of the
  40.  * original text files, but at least it more-or-less minimizes the 
  41.  * space needed while reducing the risk of leaving the user with
  42.  * indices in a messed-up state if there proves to be insufficient
  43.  * disk space available....
  44.  *
  45.  *  The structure of a 'brwsr' index is very simple; see source code
  46.  * of that program for details ... in brief, the text file is plain
  47.  * vanilla text, the key file is an array of 28-letter distinct words
  48.  * in alphabetical order, each one followed by the 4-byte cumulative count
  49.  * of how many words (including that word) have preceded it ... that
  50.  * cumulative count thus gives an offset into the ptr file, which
  51.  * simply consists of 4-byte pointers into the text file for the first
  52.  * character of each word in the index.
  53.  *
  54.  * Written in Lightspeed C ... portions therefore copyright, no doubt....
  55.  * Please send improvements and suggestions to me:
  56.  *    science (at) NEMS.ARPA;
  57.  *    [75066,2044] on CompuServe;
  58.  *    Mark^Zimmermann, 9511 Gwyndale Dr., Silver Spring, MD  20910, USA;
  59.  *    telephone 301-565-2166.
  60.  *
  61.  * Thank you!  ^z
  62.  */
  63.  
  64.  
  65. #include "mergeIndices.z.h"
  66. #include <EventMgr.h>
  67.  
  68. /* set up global stuff here:  first, size of fundamental buffer
  69.  */
  70.  
  71. long zbufsiz;
  72.  
  73. /* ...six global buffer pointers to use; need them for merging key and
  74.  * ptr file pairs:  four for input, two for output.  A single big
  75.  * buffer will serve for merging text files, and it can be defined
  76.  * locally after the key and ptr files are handled, so no need for it
  77.  * here...  For each buffer 'buf', have a moving pointer bufp and a
  78.  * counter bufcount.  Also define a global variable here to hold the
  79.  * size of the first input text file, so that ptrs can be easily
  80.  * fixed for the second text file's new offset as they are merged in....
  81.  */
  82. char *buf[3][2], *bufp[3][2];
  83. long bufcount[3][2], first_text_file_size;
  84.  
  85. /* ...global file names, vRefs and refNums... numbering scheme is:
  86.  *  first subscript is the file name (first in, second in, or merged out)
  87.  *  second subscript is the file type (ptr, key, or text)
  88.  *  (see header file for #defines to make the subscripts readable)
  89.  */
  90.  
  91. Str255 fn[3][3];
  92. int vRef[3][3], refNum[3][3];
  93.  
  94. /* offsets for the window from the top left screen corner ... 0,0 is
  95.  * fine for the Mac Plus sized screen....
  96.  */
  97. int x0, y0;
  98.  
  99. /* ...and a little window to give info to the user...
  100.  */
  101. WindowRecord w_record;
  102. WindowPtr info_window;
  103.  
  104.  
  105. /* main routine to do it all....
  106.  */
  107.  
  108. pascal void main (paramPtr) 
  109.   XCmdBlockPtr paramPtr;
  110.   {
  111.       Handle answer;
  112.       int i,j;
  113.       
  114.       SetUpA4();
  115.     if (    init_everything (paramPtr)        &&
  116.             open_all_files ()                &&
  117.             merge_key_and_ptr_files ()        &&
  118.             delete_old_key_and_ptr_files ()    &&
  119.             merge_text_files ()                &&
  120.             delete_and_rename_text_files ()        )
  121.       {
  122.         answer = NewHandle (2);
  123.         **answer = '1';
  124.         *(*answer + 1) = '\0';
  125.         paramPtr->returnValue = answer;
  126.       }
  127.     else
  128.       {
  129.         for (i = 0; i < 3; ++i)
  130.             for (j = 0; j < 3; ++j)
  131.                 if (refNum[i][j] != 0)
  132.                     FSClose (refNum[i][j]);
  133.         for (i = 0; i < 3; ++i)
  134.             for (j = 0; j < 2; ++j)
  135.                 if (buf[i][j] != 0)
  136.                     DisposPtr (buf[i][j]);
  137.       }
  138.     CloseWindow (info_window);
  139.     RestoreA4();
  140.     return;
  141.   }
  142.  
  143.  
  144. /* tiny routine to put a message into my message window.... takes in a
  145.  * PASCAL type string and displays it nicely....
  146.  */
  147.  
  148. void give_msg (msg)
  149.   char *msg;
  150.   {
  151.     Rect b_rect;
  152.  
  153.     if (info_window == 0)
  154.         return;
  155.  
  156.     b_rect.top = b_rect.left = 0;
  157.     b_rect.bottom = b_rect.right = 512;
  158.  
  159.     EraseRect (&b_rect);
  160.     MoveTo (4, 15);
  161.     DrawString (msg);
  162.     
  163.     return;
  164.   }
  165.  
  166.  
  167. /* function to beep and then wait for a mouse click before returning...
  168.  * delay for 2 ticks to debounce the mouse button a bit .... flush the
  169.  * event queue to keep from having extra clicks sent back to HC when
  170.  * we return....
  171.  */
  172.  
  173. void beepWait ()
  174.   {
  175.       long tickcount;
  176.  
  177.       SysBeep (10);
  178.       while (! Button())
  179.         ;
  180.     Delay (2L, &tickcount);
  181.     while (Button())
  182.         ;
  183.     Delay (2L, &tickcount);
  184.     FlushEvents (everyEvent, 0);
  185.     return;
  186.   }
  187.  
  188.  
  189. /* function to convert alphabetic string to a long integer ... from LSC
  190.  * library.... simplified to avoid using isspace() & isdigit() ....
  191.  */
  192.  
  193. long atol (s)
  194.   register char *s;
  195.   {
  196.     register char signflag = 0;
  197.     register long r = 0;
  198.  
  199.     while ((*s == ' '))
  200.         s++;
  201.         
  202.     if (*s == '-')
  203.       {
  204.         signflag = 1;
  205.         s++;
  206.       }
  207.     else if (*s == '+')
  208.          s++;
  209.  
  210.     while (*s >= '0' && *s <= '9') 
  211.         r = r * 10 + (*s++ - '0');
  212.     
  213.     return (signflag ? -r : r);
  214. }
  215.  
  216.  
  217.  
  218.